package net.hvacapp.cmvrfforandroid.ui.resource;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;

import net.hvacapp.cmvrfforandroid.R;
import net.hvacapp.cmvrfforandroid.adapter.BaseAdapterHelper;
import net.hvacapp.cmvrfforandroid.adapter.QuickAdapter;
import net.hvacapp.cmvrfforandroid.http.BasicHttpClient;
import net.hvacapp.cmvrfforandroid.http.HttpCallbackHandle;
import net.hvacapp.cmvrfforandroid.http.HttpConstant;
import net.hvacapp.cmvrfforandroid.manager.AccountManager;
import net.hvacapp.cmvrfforandroid.ui.BaseActivity;
import net.hvacapp.cmvrfforandroid.ui.WebviewActivity;
import net.hvacapp.cmvrfforandroid.ui.mine.model.StatisticsData;
import net.hvacapp.cmvrfforandroid.ui.product.view.PropertyItemView;
import net.hvacapp.cmvrfforandroid.ui.resource.model.GetResourceDataResp;
import net.hvacapp.cmvrfforandroid.ui.resource.model.GroupResourceData;
import net.hvacapp.cmvrfforandroid.ui.resource.model.ResourceData;
import net.hvacapp.cmvrfforandroid.utils.AnnotationUtil;
import net.hvacapp.cmvrfforandroid.widget.CellSeperateLineView;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import java.io.IOException;

@EActivity(R.layout.activity_statistics_list)
public class ExperienceListActivity extends BaseActivity {

    @ViewById(R.id.lv_templete)
    ListView mTempleteLv;

    private QuickAdapter<GroupResourceData> mQuickAdapter;

    private int mType;



    /**
     * 页面加载完后执行
     */
    @AfterViews
    @Override
    protected void viewDidLoad() {
        super.viewDidLoad();
        initViews();
        initEvent();
    }

    /***
     * 初始化视图
     */
    private void initViews() {

    }

    /**
     * 初始化
     */
    @Override
    public void initBoot() {
        setupToolbar(0, "数据统计", null);

        if (mQuickAdapter == null){
            mQuickAdapter = new QuickAdapter<GroupResourceData>(context, R.layout.product_detail_cell_item) {
                @Override
                protected void convert(final BaseAdapterHelper helper, final GroupResourceData item) {

                    LinearLayout swjInfoLl = helper.getView(R.id.ll_product_group);
                    swjInfoLl.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            //控制显示隐藏
                            item.setClosed(!item.isClosed());
                            if (item.isClosed()) {
                                helper.setImageResource(R.id.iv_selected_mark, R.drawable.arrow_down);
                                helper.setVisible(R.id.ll_property_content, false);
                            }
                            else {
                                helper.setImageResource(R.id.iv_selected_mark, R.drawable.arrow_up);
                                helper.setVisible(R.id.ll_property_content, true);
                            }
                        }
                    });

                    helper.setText(R.id.tv_title, item.getGroup());

                    //添加属性
                    LinearLayout propertyContent = helper.getView(R.id.ll_property_content);
                    propertyContent.removeAllViews();
                    if (item.getResourceDatas().size() > 0) {
                        propertyContent.setVisibility(View.VISIBLE);
                    }
                    else {
                        propertyContent.setVisibility(View.GONE);
                    }

                    for (final ResourceData property : item.getResourceDatas()) {
                        CellSeperateLineView line = new CellSeperateLineView(ExperienceListActivity.this);
                        propertyContent.addView(line);

                        final PropertyItemView itemView = new PropertyItemView(ExperienceListActivity.this);
                        itemView.setTitleString(property.getTitle());
                        itemView.hideValueTextView();

                        itemView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if (mType != 3) {
                                    Intent intent = new Intent(ExperienceListActivity.this, AnnotationUtil.get(WebviewActivity.class));
                                    intent.putExtra("title", property.getTitle());
                                    intent.putExtra("url", property.getUrl());
                                    startActivity(intent);
                                }
                                else {
                                    if (property.getUrl().contains("http")) {
                                        Intent intent = new Intent(ExperienceListActivity.this, AnnotationUtil.get(WebviewActivity.class));
                                        intent.putExtra("title", property.getTitle());
                                        intent.putExtra("url", property.getUrl());
                                        startActivity(intent);
                                    }
                                    else {
                                        //播放视频
                                        String url;
                                        if (property.getUrl().contains("1")) {
                                            url = "android.resource://" + getPackageName() + "/" + R.raw.video1;
                                        }
                                        else {
                                            url = "android.resource://" + getPackageName() + "/" + R.raw.video2;
                                        }

                                        Intent intent = new Intent(ExperienceListActivity.this, AnnotationUtil.get(VideoPlayerActivity.class));
                                        intent.putExtra("title", property.getTitle());
                                        intent.putExtra("url", url);
                                        startActivity(intent);

//                                        Uri uri = Uri.parse(url);
//                                        //调用系统自带的播放器
//                                        Intent intent = new Intent(Intent.ACTION_VIEW);
//                                        intent.setDataAndType(uri, "video/mp4");
//                                        startActivity(intent);
                                    }
                                }
                            }
                        });

                        propertyContent.addView(itemView);

                    }
                }
            };
        }

        mTempleteLv.setAdapter(mQuickAdapter);

    }


    /**
     * 初始化数据
     */
    @Override
    public void initData(Intent intent) {

        mType = intent.getIntExtra("type", 0);

        if (mType == 0) {
            setupToolbar(0, "产品系列介绍", null);
        }
        else if (mType == 1) {
            setupToolbar(0, "厂家介绍", null);
        }
        else if (mType == 2) {
            setupToolbar(0, "样板工程介绍", null);
        }
        else {
            setupToolbar(0, "实景体验", null);
        }

        loadResourceListData(mType);
    }



    /**
     * 页面名称
     */
    @Override
    public String getName() {
        return "";
    }



    private void initEvent() {

        mTempleteLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                StatisticsData data = (StatisticsData) parent.getItemAtPosition(position);

                Intent intent = new Intent(ExperienceListActivity.this, AnnotationUtil.get(WebView.class));
                Bundle bundle = new Bundle();
                bundle.putString("url", data.getGuidId());
                bundle.putString("title", data.getTitle());
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });


    }


    private void loadResourceListData(final int type) {
        String url;
        if (type == 0) {
            url = HttpConstant.HTTP_GET_PRODUCT_DOC;
        }
        else if (type == 1) {
            url = HttpConstant.HTTP_GET_MANUFACTURE_DOC;
        }
        else if (type == 2) {
            url = HttpConstant.HTTP_GET_DEMO_PROJECT;
        }
        else {
            url = HttpConstant.HTTP_GET_DEMO_ANIMATION;
        }

        BasicHttpClient.httpGetAsync(url, AccountManager.getInstance().getToken(), new HttpCallbackHandle<GetResourceDataResp>() {
            @Override
            public void onSuccess(GetResourceDataResp obj) {
                // 成功
                if (type == 0) {
                    mQuickAdapter.addAll(obj.getGetProductUrlResult());
                }
                else if (type == 1) {
                    mQuickAdapter.addAll(obj.getGetManufactureUrlResult());
                }
                else if (type == 2) {
                    mQuickAdapter.addAll(obj.getGetSampleProjectUrlResult());
                }
                else {
                    mQuickAdapter.addAll(obj.getGetAnimationUrlResult());
                }
            }

            @Override
            public void onFailure(String response) {
                showMessage("获取模板失败，请重试！");
            }
        });
    }


}
